package com.woniuxy.authapp.service.domain;


import com.woniuxy.userapp.client.param.UserInfo;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;

@Data
public class SecurityUser implements UserDetails {

    private Integer id;
    /**
     *用户名
     */
    private String userName;
    /**
     * 密码
     */
    private String pwd;
    /**
     * 客户端登录id,暂时留着,看有用没
     */
    private String clientId;


    private boolean enabled;

    private Collection<SimpleGrantedAuthority> authorities;


    public SecurityUser(UserInfo userInfo){
        this.setId(userInfo.getId());
        this.setEnabled(userInfo.getState() == 1);
        this.setUserName(userInfo.getUserName());
        this.setPwd(userInfo.getPassword());
        this.setClientId(userInfo.getClientId());
        if(userInfo.getRoles() != null){
            authorities = new ArrayList<>();
            userInfo.getRoles().forEach((item)->authorities.add(new SimpleGrantedAuthority(item)));
        }
    }


    /**
     * 角色权限
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.pwd;
    }

    @Override
    public String getUsername() {
        return this.userName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
